package com.luck.emp.wh.logic;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;

import com.luck.emp.common.SpWarehouseNo;
import com.luck.emp.entity.EmpTSalesman;
import com.luck.emp.entity.EmpTWarehouse;
import com.luck.emp.wh.qry.WhQry;
import com.luck.pf.common.entity.UserBean;
import com.luck.pf.framework.dao.BaseJdbcDao;
import com.luck.pf.framework.logic.BaseLogic;
import com.luck.pf.util.CriteriaUtil;
import com.luck.pf.util.DateUtil;
import com.luck.pf.util.Page;

/**
 * 说明 Copyright: Copyright (c) 2011 深圳**科技
 * 
 * @author
 * 
 */
public class WhLogic extends BaseLogic implements IWhLogic {
	private BaseJdbcDao jdbcDao;

	/**
	 * 查找分页信息
	 */
	public Page findWhPage(WhQry qry) {
		Criteria criteria = baseDao.createCriteria(EmpTWarehouse.class);
		CriteriaUtil.addEq(criteria, "whCode", qry.getWhCode());
		CriteriaUtil.addEq(criteria, "whName", qry.getWhName());
		
		Order order = Order.desc("whCode");
		criteria.addOrder(order);
		if(qry.getIsSub().equals("1")){
			criteria.createAlias("dept", "d");
			filterData(criteria, qry.getUserInfo(), "d", null);
		}else
			CriteriaUtil.addEq(criteria, "deptId", qry.getUserInfo().getDeptId());
		return baseDao.pagedQuery(criteria, qry.getPageNo(), qry.getPageSize());
	}
	public List<EmpTWarehouse> findWarehouse(Long deptId, String isDefault){
		Criteria criteria = baseDao.createCriteria(EmpTWarehouse.class);
		CriteriaUtil.addEq(criteria, "deptId", deptId);
		if(StringUtils.isNotBlank(isDefault) && isDefault.equals("1")){
			CriteriaUtil.addEq(criteria, "isDefault", "1");
		}
		return criteria.list();
	}
	public EmpTWarehouse findDefaultWarehouse(Long deptId){
		List<EmpTWarehouse> ls = findWarehouse(deptId, "1");
		if(ls != null && ls.size()>0)
			return ls.get(0);
		return null;
	}
	/**
	 * 保存
	 **/
	public String saveWh(UserBean userBean, EmpTWarehouse entity) throws Exception {
		entity.setModiDate(DateUtil.getDate());
		entity.setModiUser(userBean.getUser());
		String whCode = entity.getWhCode();
		if(StringUtils.isBlank(entity.getWhCode())){//新建
			entity.setCreateDate(DateUtil.getDate());
			entity.setCreateUser(userBean.getUser());
			SpWarehouseNo sp = new SpWarehouseNo(this.jdbcDao.getJdbcTemplate());
			whCode = sp.execute(userBean.getDept().getDeptCode());
			entity.setWhCode(whCode);
			entity.setDeptId(userBean.getDeptId());
			entity.setStatus("1");
		}
		super.save(entity);
		return null;
	}

	/**
	 * 删除
	 */
	public String deleteWh(UserBean userBean, EmpTWarehouse entity) throws Exception {
		StringBuffer bf = new StringBuffer();
		if(entity.getWhMode().equals("1"))
			bf.append("<br>不能删除默认仓库");
		entity = super.get(EmpTWarehouse.class, entity.getWhCode());
		if(!entity.getDeptId().equals(userBean.getDeptId()))
			bf.append("<br>不属于您所在的机构");

		int count = super.baseDao.count("  select count(*) from EmpTWhOrderM t where t.whCode = ?",entity.getWhCode());
		if (count > 0)
			bf.append("<br>此仓库有出入库记录");
		if(StringUtils.isNotBlank(bf.toString())){
			throw new Exception(bf.toString());
		}
		super.delete(entity);
		return null;
	}

	public BaseJdbcDao getJdbcDao() {
		return jdbcDao;
	}

	public void setJdbcDao(BaseJdbcDao jdbcDao) {
		this.jdbcDao = jdbcDao;
	}

}